/**
 * @file RTT_right_value.cpp
 * @author tttt (tttt@domain.com)
 * @brief 这段代码要在C++14环境下编！ g++ -std=c++11 RTT_right_value.cpp
 * @version 0.1
 * @date 2023-09-15
 * 
 * @copyright Copyright (c) 2023
 * 
 */
#include <iostream>
#include <memory>

namespace impl {
    class BaseClassImpl {
    public:
        virtual void doSomething() = 0;
        virtual ~BaseClassImpl() = default;
    };

    class DerivedClassImpl : public BaseClassImpl {
    public:
        void doSomething() override {
            std::cout << "DerivedClassImpl is doing something!" << std::endl;
        }
    };

    class ttttImpl : public BaseClassImpl {
    public:
        void doSomething() override {
            std::cout << "ttttImpl is doing something!" << std::endl;
        }
    };

}

class BaseClass {
public:
    // 注意这里`impl`是命名空间
    // 这里把BaseClassImpl的实现权限给了BaseClass，而DerivedClassImpl
    // 是BaseClassImpl的实现，所以调用了DerivedClassImpl
    BaseClass(std::unique_ptr<impl::BaseClassImpl>&& impl)
        : pImpl(std::move(impl)) {}
    // explicit BaseClass();

    void doSomething() {
        pImpl->doSomething();
    }

private:
    std::unique_ptr<impl::BaseClassImpl> pImpl;
};

int main() {
    std::unique_ptr<impl::BaseClassImpl> derivedImpl = std::make_unique<impl::DerivedClassImpl>();
    //std::unique_ptr<impl::BaseClassImpl> ttttImpl = std::make_unique<impl::ttttImpl>();
    auto ttttImpl = std::make_unique<impl::ttttImpl>();

    BaseClass base(std::move(derivedImpl));
    BaseClass tttt_base(std::move(ttttImpl));
    base.doSomething();
    tttt_base.doSomething();

    return 0;
}
